const Response = require('@/utils/response');
const CaptchaService = require('@/service/captcha');

class CaptchaController {

  async getCaptcha(ctx) {
    const captcha = await CaptchaService.generate();
    Response.success(ctx, captcha);
  }
  async verifyCaptcha(ctx, next) {
    const { captcha, captchaKey } = ctx.request.body;
    // const { key, code} = ctx.request.body;
    if (!captcha || !captchaKey) {
      return Response.error(ctx, '验证码不能为空', 400);
    }
    const isValid = await CaptchaService.verify(captcha, captchaKey);
    if (isValid) {
      return Response.success(ctx, '验证成功');
    } else {
      return Response.error(ctx, '验证失败', 400);
    }
  }
}
module.exports = new CaptchaController();